import Shapes.Line;
import Shapes.Oval;
import Shapes.Rectangle;
import Shapes.Shape;

import javax.management.remote.JMXServiceURL;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-12-16
 * Time: 13:04
 */


public class MyPanel extends JPanel{

    //存储所有已经画好的形状的数组
    public static ArrayList<Shape> arrayList=new ArrayList<>();

    //鼠标监听事件,用来让按钮,画图板添加
    public static Listener listener=new Listener();

    /**
     * 设置页面
     * @param shapes  从配置文件中读取的形状
     */
    public static void init(String[] shapes) {
        //将维护的数组传给 监听类,好让他们进行添加
        listener.setArrayList(arrayList);

        //设置frame
        JFrame jFrame = new JFrame("画图板");
        jFrame.setSize(1300,850);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLocationRelativeTo(null);
        jFrame.setLayout(new BorderLayout());
        jFrame.setResizable(false);

        //设置菜单
        JMenuBar jMenuBar=new JMenuBar();
        JMenu jMenu=new JMenu("文件");
        jMenu.setFont(new Font("宋体",Font.BOLD,20));
        JMenuItem jMenuItem1=new JMenuItem("保存");
        JMenuItem jMenuItem2=new JMenuItem("导入");
        jMenuItem1.setFont(new Font("宋体",Font.BOLD,20));
        jMenuItem2.setFont(new Font("宋体",Font.BOLD,20));
        //菜单添加监听事件
        jMenuItem1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog jDialog=new JDialog();
                jDialog.setBounds(300,100,300,300);
                JLabel jLabel=new JLabel("老师好,这个保存和导入功能还没有实现");
                jLabel.setFont(new Font("宋体",Font.BOLD,15));
                jDialog.add(jLabel);
                jDialog.setVisible(true);
            }
        });
        jMenu.add(jMenuItem1);
        jMenu.add(jMenuItem2);
        jMenuBar.add(jMenu);
        jFrame.setJMenuBar(jMenuBar);

        //关于选择形状的按钮
        JPanel northPanel=new JPanel();
        northPanel.setBackground(Color.LIGHT_GRAY);
        northPanel.setPreferredSize(new Dimension(1300,80));
        //不仅要将配置文件中有的形状添加进去,还要添加 曲线 橡皮
        ArrayList<String> shapeArr=new ArrayList<>();
        shapeArr.add("Curve");
        for (String str: shapes) {
            shapeArr.add(str);
        }
        shapeArr.add("Eraser");
        //开始设置关于曲线的按钮
        for (String str: shapeArr) {
            JButton jButton=new JButton(str);
            jButton.setPreferredSize(new Dimension(100,60));
            jButton.addActionListener(listener);
            jButton.setFont(new Font("宋体",Font.BOLD,20));
            northPanel.add(jButton);
        }
        jFrame.add(northPanel,BorderLayout.NORTH);


        //设置选择画笔粗细的按钮
        JPanel leftPanel=new JPanel(new GridLayout(10,1));
        leftPanel.setSize(80,650);
        String[] sizes=new String[]{"小号线","中号线","大号线"};
        for (String str: sizes) {
            JButton jButton=new JButton(str);
            jButton.setPreferredSize(new Dimension(100,60));
            jButton.addActionListener(listener);
            jButton.setFont(new Font("宋体",Font.BOLD,20));
            leftPanel.add(jButton);
        }
        jFrame.add(leftPanel,BorderLayout.WEST);


        //设置选择画笔颜色的按钮
        JPanel colorPanel=new JPanel();
        Color[] colors=new Color[]{
                Color.BLACK,Color.BLUE,Color.cyan,Color.GRAY,Color.ORANGE,
                Color.green, Color.magenta,Color.RED,Color.white
        };
        for(int i=0;i<colors.length;i++){
            JButton colorButton=new JButton();
            colorButton.setBackground(colors[i]);
            colorButton.setPreferredSize(new Dimension(50,50));
            colorButton.addActionListener(listener);
            colorPanel.add(colorButton);
        }
        jFrame.add(colorPanel,BorderLayout.SOUTH);


        //中间用来画画的面板
        MyPanel centerPanel=new MyPanel();
        //将该面板传给 监听类,监听类才能调用repaint
        listener.setPanel(centerPanel);
        jFrame.add(centerPanel,BorderLayout.CENTER);
        centerPanel.setBackground(Color.WHITE);
        //画板添加监听事件才能画
        centerPanel.addMouseListener(listener);
        centerPanel.addMouseMotionListener(listener);
        //setVisible放在getGraphics之前画笔才不是空
        jFrame.setVisible(true);
        listener.setGraphics(centerPanel.getGraphics());
    }


    /**
     * 重绘
     * @param g  the <code>Graphics</code> context in which to paint
     */
    @Override
    public void paint(Graphics g) {
        //先调用父类方法,将画板清空
        super.paint(g);
        //将之前画过的重新添加进去
        for (Shape s: arrayList) {
            s.draw(g);
        }
        //再画当前要画的图形
        switch (listener.type){
            //如果是曲线,将他当作一小段一小段的直线进行画,并进行存储
            case "Curve":
                Line line=new Line(listener.x1, listener.y1, listener.x2,listener.y2,
                        listener.color, listener.size);
                line.draw(listener.graphics);
                listener.arrayList.add(line);
                listener.x1=listener.x2;
                listener.y1=listener.y2;
                break;
                //其他的形状只用画一下就行,不用再存储,因为鼠标还在移动,只要确定最后释放的位置,
            //在那个位置画就行
            case "Line":
                new Line(listener.x1,listener.y1,listener.x2,listener.y2,
                        listener.color,listener.size).draw(listener.graphics);
                break;
            case "Rect":
                new Rectangle(listener.x1,listener.y1,listener.x2,listener.y2,
                        listener.color,listener.size).draw(listener.graphics);
                break;
            case "Oval":
                new Oval(listener.x1,listener.y1,listener.x2,listener.y2,
                        listener.color,listener.size).draw(listener.graphics);
                break;
                //如果是橡皮,就要将画笔颜色改为和背景颜色相同
            //并像曲线那样,看作由许多小的直线组成,并进行存储
            case "Eraser":
                Line eraser=new Line(listener.x1,listener.y1,listener.x2,listener.y2,
                        Color.WHITE,40);
                eraser.draw(listener.graphics);
                listener.arrayList.add(eraser);
                listener.x1=listener.x2;
                listener.y1=listener.y2;
                break;
        }
    }
}
